package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.base.BaseResult;
import com.ls.base.BaseResultUtil;
import com.ls.controller.model.channelIn.AddChannelInModel;
import com.ls.controller.model.channelIn.QueryChannelInModel;
import com.ls.entity.CommChannel;
import com.ls.entity.SmsChannelIn;
import com.ls.entity.SysUser;
import com.ls.exception.SysException;
import com.ls.mapper.CommChannelMapper;
import com.ls.mapper.SmsChannelInMapper;
import com.ls.service.ISmsChannelInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.utils.PagingQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

import static com.ls.base.BaseResultUtil.SQL_ERROR;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pengyu
 * @since 2019-12-05
 */
@Service
public class SmsChannelInServiceImpl extends ServiceImpl<SmsChannelInMapper, SmsChannelIn> implements ISmsChannelInService {

    @Autowired
    SmsChannelInMapper mapper;

    @Autowired
    CommChannelMapper channelMapper;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = SysException.class)
    @Override
    public BaseResult add(AddChannelInModel model, SysUser currentUser) {
        Long chanId = model.getChanId();
        CommChannel commChannel = channelMapper.selectById(chanId);
        if(commChannel==null){
            return BaseResultUtil.error("chanId："+chanId+" 在库里不存在");
        }
        int inNo = model.getInNo();
        if(inNo<0 || inNo >99999){
            return BaseResultUtil.error("inNo 太小或太大："+inNo);
        }
        SmsChannelIn smsChannelIn = new SmsChannelIn();
        smsChannelIn.setChanId(chanId);
        smsChannelIn.setInNo(inNo);
        smsChannelIn.setAfterStock(commChannel.getStock() + inNo);
        smsChannelIn.setCreateTime(LocalDateTime.now());
        smsChannelIn.setCreateUserId(currentUser.getUserId());
        smsChannelIn.setUpdateTime(LocalDateTime.now());
        smsChannelIn.setUpdateUserId(currentUser.getUserId());
        int result = mapper.insert(smsChannelIn);
        if(result>0){
            commChannel.setStock(commChannel.getStock() + inNo);
            commChannel.setTotalNo(commChannel.getTotalNo() + inNo);
            channelMapper.updateById(commChannel);
            return BaseResultUtil.success();
        }else{
            return BaseResultUtil.error(SQL_ERROR);
        }
    }

    @Override
    public BaseResult list(QueryChannelInModel model, SysUser currentUser) {
        QueryWrapper<SmsChannelIn> wrapper = new QueryWrapper<SmsChannelIn>().eq("chan_id",model.getChanId());
        IPage<SmsChannelIn> pageData = mapper.selectPage(new Page<>(model.getPageNum(), model.getPageSize()), wrapper);
        return BaseResultUtil.success(pageData);
    }


}
